	#include "fx2.h"
	#include "fx2regs.h"
	#include "ov5647.h"
	#include "sccb.h"
	#define CALIBRATION

	

void OV5647_Sensor_Init(void)
{

	OV5647_reg_write(0x0100, 0x00);		/*软件休眠*/
	OV5647_reg_write(0x0103, 0x01);		/*软件复位*/
	/*延时5ms*/
	EZUSB_Delay(5);
	OV5647_reg_write( 0x370c, 0x03 );

	/* 电源配置 */
	OV5647_reg_write(0x3013, 0x08);		/*使用外部DVDD供电禁用内部降压器*/

	/* 镜头阴影校正 黑像素校正 白像素校正 */ 
	OV5647_reg_write( 0x5000, 0x03 );    /* 白像素校正 */        
	
	/*开自动白平衡*/
	OV5647_reg_write( 0x5001, 0x00 );     /* AWB disable  */            
	OV5647_reg_write( 0x5002, 0x41 );   /* AWB Gain Enable */             
	OV5647_reg_write( 0x5003, 0x08 );   /* buf_en */ 
	OV5647_reg_write( 0x5005, 0x31 );
	OV5647_reg_write( 0x5025, 0x00 );
	OV5647_reg_write( 0x5046, 0x09 );

	#if 1            
	OV5647_reg_write( 0x5180, 0x08 );   /* 输出gains值*/ 
	OV5647_reg_write( 0x5186, 0X04 );	/* manual red gain high*/
	OV5647_reg_write( 0x5187, 0X00 );	/* manual red gain low*/
	OV5647_reg_write( 0x5188, 0X04 );	/* manual green gain high*/
	OV5647_reg_write( 0x5189, 0X00 );	/* manual green gain low*/
	OV5647_reg_write( 0x518A, 0X04 );	/* manual blue gain high*/
	OV5647_reg_write( 0x518B, 0X00 );	/* manual blue gain low*/
	OV5647_reg_write( 0x5000, 0X06 );	/* lenc off, bpc on, wpc on*/
	#endif           
	
	OV5647_reg_write( 0x5a00, 0x08 );   /*Debug mode ensable */


	/* IO配置	 */	
	OV5647_reg_write(0x3000, 0x0f);		/*D[9:8] output*/
	OV5647_reg_write(0x3001, 0xfc);	   	/* D[7:0] output*/			
	OV5647_reg_write(0x3002, 0xe4);		/* Vsync, Href, PCLK, Frex, Strobe, SDA, GPIO1, GPIO0 */	

	
	/* 输出时序配置 */
	/*OV5647_reg_write(0x4708, 0x03);*/	/*反相VSYNC*/		

	/* 输出模式设置 */
	/* OV5647_reg_write(0x3034, 0x18); */	 	/*设置为8bit输出模式*/	
			
	/* PLL*/
	OV5647_reg_write(0x3035 , 0x21 );	
	OV5647_reg_write(0x3036 , 0x18 );	  
	OV5647_reg_write(0x303c , 0x12 );			
    OV5647_reg_write(0x4837 , 0x24 );     /*pclk period */ 
  
	/* analog control*/ 			
	OV5647_reg_write( 0x3827, 0xec );        
 
	/* 这个寄存器配置在增益上限前面*/            
	OV5647_reg_write( 0x301d, 0xf0 ); 
	/*--------------------------------------------------- 
	**增益上限
	** 增益上限由{0x3a18[9:8],0x3a19[7:0]}控制，
	** gain_ceiling={0x3a18[9:8],0x3a19[7:0]}/16
	** 增益上限设置为15.5
	---------------------------------------------------*/
	OV5647_reg_write( 0x3a18, 0x00 );   /* Bit[1:0]: gain_ceiling[9:8]  */              
	OV5647_reg_write( 0x3a19, 0xf8 );   /* Bit[7:0]: gain_ceiling[7:0]  */
	
	OV5647_reg_write( 0x3c01, 0x80 );   /* 波段检测手动模式   */         
	OV5647_reg_write( 0x3b07, 0x0c );   /* 频闪模式：帧曝光模式(FREX)	*/
	
	
	/*analog control*/
	OV5647_reg_write( 0x3630, 0x2e );                
	OV5647_reg_write( 0x3632, 0xe2 );                
	OV5647_reg_write( 0x3633, 0x23 );                
	OV5647_reg_write( 0x3634, 0x44 );                
	OV5647_reg_write( 0x3636, 0x06 );                
	OV5647_reg_write( 0x3620, 0x64 );                
	OV5647_reg_write( 0x3621, 0xe0 );                
	OV5647_reg_write( 0x3600, 0x37 );                
	OV5647_reg_write( 0x3704, 0xa0 );                
	OV5647_reg_write( 0x3703, 0x5a );               
	OV5647_reg_write( 0x3715, 0x78 );                
	OV5647_reg_write( 0x3717, 0x01 );                
	OV5647_reg_write( 0x3731, 0x02 );                
	OV5647_reg_write( 0x370b, 0x60 );                
	OV5647_reg_write( 0x3705, 0x1a );                	
	OV5647_reg_write( 0x3f05, 0x02 );                
	OV5647_reg_write( 0x3f06, 0x10 );                
	OV5647_reg_write( 0x3f01, 0x0a );

	/* 平均算法 */              
	OV5647_reg_write( 0x3a0f, 0x58 );	/* stable in high  	*/           
	OV5647_reg_write( 0x3a10, 0x50 );   /* stable in low   	*/        
	OV5647_reg_write( 0x3a1b, 0x58 );   /* stable out high 	*/          
	OV5647_reg_write( 0x3a1e, 0x50 );   /* stable out low  	*/         
	OV5647_reg_write( 0x3a11, 0x60 );   /* fast zone high  	*/        
	OV5647_reg_write( 0x3a1f, 0x28 );	/* fast zone low 	*/
	
	OV5647_reg_write( 0x0100 ,0x01 );	/* wake up from software standby	 */
	
	/* black level calibration	 */	
	OV5647_reg_write( 0x4000, 0x89 );   /* BLC enable  	    */ 		
	OV5647_reg_write( 0x4001, 0x02 );   /* BLC start line	*/
	OV5647_reg_write( 0x4002, 0xc5 );
  	OV5647_reg_write( 0x4004, 0x06 );
  	OV5647_reg_write( 0x4005, 0x1a );
  
	
	/*Gain has no latch delay, AGC manual, AEC manual*/	
	OV5647_reg_write( 0x3503, 0x13 );	
	
	/*----------------------------------------------------------- 
	** Exposure=0x3500 [3:0]×4096+0x3501[7:0]×256+0x3502[7: 4] 
	** The maximum exposure value is limited by VTS defined by register 0x380e and 0x380f.
	** Maximum Exposure <= VTS -4
	------------------------------------------------------------*/
	OV5647_reg_write( 0x3500, 0x03 );   /* exposure[19:16]	*/	
	OV5647_reg_write( 0x3501, 0x50 );   /* exposure[15:8]	*/	
	OV5647_reg_write( 0x3502, 0xb0 );   /* exposure[7:0]		*/		
	/*-------------------real gain---------------------------------- 
	** Gain=R0x350a[1:0]×16 + R0x350b [7 :0 ]/16
	** 设置为1
	--------------------------------------------------------------*/
	OV5647_reg_write( 0x350a, 0x00 );   /* gain[9:8] */	
	OV5647_reg_write( 0x350b, 0x50 );   /* gain[7:0] */	
	//OV5647_reg_write( 0x350c, 0x00 );   /* vts diff manual vts set to 0 */
  	//OV5647_reg_write( 0x350d, 0x00 );   /* vts diff manual vts set to 0 */
  	//OV5647_reg_write( 0x3011, 0x22 );   /* bit[6:5] drive strength */


	#ifdef CALIBRATION
	OV5647_reg_write( 0x5000, 0x86 );   /* 镜头光学校正 */
	OV5647_reg_write( 0x5800, 0x00 );
	OV5647_reg_write( 0x5801, 0x00 );
	OV5647_reg_write( 0x5802, 0x00 );
	OV5647_reg_write( 0x5803, 0x00 );
	OV5647_reg_write( 0x5804, 0x00 );
	OV5647_reg_write( 0x5805, 0x00 );
	OV5647_reg_write( 0x5806, 0x00 );
	OV5647_reg_write( 0x5807, 0x00 );
	OV5647_reg_write( 0x5808, 0x00 );
	OV5647_reg_write( 0x5809, 0x00 );
	OV5647_reg_write( 0x580a, 0x00 );
	OV5647_reg_write( 0x580b, 0x00 );
	OV5647_reg_write( 0x580c, 0x00 );
	OV5647_reg_write( 0x580d, 0x00 );
	OV5647_reg_write( 0x580e, 0x00 );
	OV5647_reg_write( 0x580f, 0x00 );
	OV5647_reg_write( 0x5810, 0x00 );
	OV5647_reg_write( 0x5811, 0x00 );
	OV5647_reg_write( 0x5812, 0x00 );
	OV5647_reg_write( 0x5813, 0x00 );
	OV5647_reg_write( 0x5814, 0x00 );
	OV5647_reg_write( 0x5815, 0x00 );
	OV5647_reg_write( 0x5816, 0x00 );
	OV5647_reg_write( 0x5817, 0x00 );
	OV5647_reg_write( 0x5818, 0x00 );
	OV5647_reg_write( 0x5819, 0x00 );
	OV5647_reg_write( 0x581a, 0x00 );
	OV5647_reg_write( 0x581b, 0x00 );
	OV5647_reg_write( 0x581c, 0x00 );
	OV5647_reg_write( 0x581d, 0x00 );
	OV5647_reg_write( 0x581e, 0x00 );
	OV5647_reg_write( 0x581f, 0x00 );
	OV5647_reg_write( 0x5820, 0x00 );
	OV5647_reg_write( 0x5821, 0x00 );
	OV5647_reg_write( 0x5822, 0x00 );
	OV5647_reg_write( 0x5823, 0x00 );
	OV5647_reg_write( 0x5824, 0xfe );
	OV5647_reg_write( 0x5825, 0xfe );
	OV5647_reg_write( 0x5826, 0xfe );
	OV5647_reg_write( 0x5827, 0xfe );
	OV5647_reg_write( 0x5828, 0xfe );
	OV5647_reg_write( 0x5829, 0xfe );
	OV5647_reg_write( 0x582a, 0xed );
	OV5647_reg_write( 0x582b, 0xed );
	OV5647_reg_write( 0x582c, 0xed );
	OV5647_reg_write( 0x582d, 0xfe );
	OV5647_reg_write( 0x582e, 0xfe );
	OV5647_reg_write( 0x582f, 0xed );
	OV5647_reg_write( 0x5830, 0xdc );
	OV5647_reg_write( 0x5831, 0xed );
	OV5647_reg_write( 0x5832, 0xfe );
	OV5647_reg_write( 0x5833, 0xfe );
	OV5647_reg_write( 0x5834, 0xed );
	OV5647_reg_write( 0x5835, 0xed );
	OV5647_reg_write( 0x5836, 0xed );
	OV5647_reg_write( 0x5837, 0xfe );
	OV5647_reg_write( 0x5838, 0xfe );
	OV5647_reg_write( 0x5839, 0xfe );
	OV5647_reg_write( 0x583a, 0xfe );
	OV5647_reg_write( 0x583b, 0xfe );
	OV5647_reg_write( 0x583c, 0xfe );
	OV5647_reg_write( 0x583d, 0xce );
	#endif	   	
}	

	
	
	
	
void OV5647_Sensor_Init_5M(void)
{

	OV5647_reg_write( 0x3820, 0x40 );  	// 传感器垂直翻转	
	OV5647_reg_write( 0x3821, 0x00 );	// ISP miror off, Sensor mirror off, bin off   		
	
	
	/*以下是配置成2592*1944格式的配置*/			   	
	OV5647_reg_write( 0x3612, 0x5B );   //analog control             
	OV5647_reg_write( 0x3618, 0x04 );  	//analog control
	OV5647_reg_write( 0x3708,0x24 ); //
  	OV5647_reg_write( 0x3709,0x12 ); //
  	OV5647_reg_write( 0x370c,0x00 ); // 
	
	
	//帧率:10FPS
	OV5647_reg_write( 0x380c, 0x0a );  	//HTS     2700      
	OV5647_reg_write( 0x380d, 0x8c );                
	OV5647_reg_write( 0x380e, 0x07 );   //VTS     1947     
	OV5647_reg_write( 0x380f, 0xb6 );
	



	//子采样配置
	OV5647_reg_write( 0x3814, 0x11 ); 	// timing X inc             
	OV5647_reg_write( 0x3815, 0x11 ); 	// timing Y inc	
	
	OV5647_reg_write( 0x3708, 0x64 );  	// analog control            
	OV5647_reg_write( 0x3709, 0x12 );  	// analog control 

	//输出图像大小配置
	OV5647_reg_write( 0x3808, 0x0a );   //DVPHO = 2592            
	OV5647_reg_write( 0x3809, 0x20 );   //DVPHO            
	OV5647_reg_write( 0x380a, 0x07 );  	//DVPVO = 1944             
	OV5647_reg_write( 0x380b, 0x98 ); 	//DVPHO
	//窗口大小配置	
	OV5647_reg_write( 0x3800, 0x00 ); 	//X Start
	OV5647_reg_write( 0x3801, 0x00 );                
	OV5647_reg_write( 0x3802, 0x00 );   //Y Start             
	OV5647_reg_write( 0x3803, 0x00 );                
	OV5647_reg_write( 0x3804, 0x0a );   //X End 	2623            
	OV5647_reg_write( 0x3805, 0x3f );                
	OV5647_reg_write( 0x3806, 0x07 );   //Y End     1955        
	OV5647_reg_write( 0x3807, 0xa3 );
	//偏移量配置
 	OV5647_reg_write( 0x3810, 0x00 );
	OV5647_reg_write( 0x3811, 0x04 );  //X偏移量
	OV5647_reg_write( 0x3812, 0x00 );	
	OV5647_reg_write( 0x3813, 0x02 );  //Y偏移量 	 	
	
	/*banding filters are calculated automatically in camera driver */           
	OV5647_reg_write( 0x3a08, 0x01 );   // B50 step             
	OV5647_reg_write( 0x3a09, 0x27 );   // B50 step             
	OV5647_reg_write( 0x3a0a, 0x00 );   // B60 step            
	OV5647_reg_write( 0x3a0b, 0xf6 );   // B60 step             
	OV5647_reg_write( 0x3a0d, 0x04 );   // 60Hz max band             
	OV5647_reg_write( 0x3a0e, 0x03 );	// 50Hz max band
		    
	OV5647_reg_write( 0x4004, 0x04 );   /* black line number   */               
	/*	OV5647_reg_write( 0x4050, 0x6e );  */	              
	/*	OV5647_reg_write( 0x4051, 0x8f );  */
	OV5647_reg_write( 0x0100 ,0x01 );	// wake up from software standby		
} 

void OV5647_Sensor_Init_960P(void)
{

	OV5647_reg_write( 0x3821, 0x00 );	// ISP miror off, Sensor mirror off, bin off   		
	OV5647_reg_write( 0x3820, 0x40 );  	// ISP flip off, Sensor flip off, bin OFF
				
	
	/*以下是配置成1280*960格式的配置*/			   
	
	
	OV5647_reg_write( 0x3612, 0x59 );   //analog control             
	OV5647_reg_write( 0x3618, 0x00 );  	//analog control 
	//帧率:10FPS 
	#if 0
	OV5647_reg_write( 0x380c, 0x12 );  	//[4:0]hts high     4800      
	OV5647_reg_write( 0x380d, 0xC0 );   //[7:0]hts low              
	OV5647_reg_write( 0x380e, 0x03 );   //[4:0]vts high     1000     
	OV5647_reg_write( 0x380f, 0xE8 );  	//[7:0]vts low  
	#endif
	//帧率:30FPS
	OV5647_reg_write( 0x380c, 0x06 );  	//HTS     1600      
	OV5647_reg_write( 0x380d, 0x40 );                
	OV5647_reg_write( 0x380e, 0x03 );   //VTS     1000     
	OV5647_reg_write( 0x380f, 0xe8 );
	//子采样配置
	OV5647_reg_write( 0x3814, 0x31 ); 	// h subsample inc              
	OV5647_reg_write( 0x3815, 0x31 ); 	// v subsample inc

	OV5647_reg_write( 0x3708, 0x64 );  	// analog control            
	OV5647_reg_write( 0x3709, 0x52 );  	// analog control

	//输出图像大小配置
	OV5647_reg_write( 0x3808, 0x05 );   //[4:0]dvp h out high
	OV5647_reg_write( 0x3809, 0x00 );   //[7:0]dvp h out low 
	OV5647_reg_write( 0x380a, 0x03 );  	//[4:0]dvp v out high
	OV5647_reg_write( 0x380b, 0xC0 ); 	//[7:0]dvp v out low 
	//窗口大小配置	
	OV5647_reg_write( 0x3800, 0x00 ); 	//[4:0]dvp h start   	
	OV5647_reg_write( 0x3801, 0x18 ); 	//[7:0]dvp h start     
	OV5647_reg_write( 0x3802, 0x00 ); 	//[4:0]dvp v start     
	OV5647_reg_write( 0x3803, 0x0e ); 	//[7:0]dvp v start     
	OV5647_reg_write( 0x3804, 0x0a ); 	//[4:0]dvp h end       
	OV5647_reg_write( 0x3805, 0x27 ); 	//[7:0]dvp h end       
	OV5647_reg_write( 0x3806, 0x07 ); 	//[4:0]dvp v end       
	OV5647_reg_write( 0x3807, 0x95 ); 	//[7:0]dvp v end     
	//偏移量配置
// 	OV5647_reg_write( 0x3810, 0x00 );
//	OV5647_reg_write( 0x3811, 0x04 );  //X偏移量
//	OV5647_reg_write( 0x3812, 0x00 );	
//	OV5647_reg_write( 0x3813, 0x02 );  //Y偏移量 	

	/*banding filters are calculated automatically in camera driver */           
	OV5647_reg_write( 0x3a08, 0x01 );   // B50 step             
	OV5647_reg_write( 0x3a09, 0x27 );   // B50 step             
	OV5647_reg_write( 0x3a0a, 0x00 );   // B60 step            
	OV5647_reg_write( 0x3a0b, 0xf6 );   // B60 step             
	OV5647_reg_write( 0x3a0d, 0x04 );   // 60Hz max band             
	OV5647_reg_write( 0x3a0e, 0x03 );	// 50Hz max band
	
//	/* AGC AEC target */              
//	OV5647_reg_write( 0x3a0f, 0x58 );	// stable in high            
//	OV5647_reg_write( 0x3a10, 0x50 );   // stable in low          
//	OV5647_reg_write( 0x3a1b, 0x58 );   // stable out high          
//	OV5647_reg_write( 0x3a1e, 0x50 );   // stable out low          
//	OV5647_reg_write( 0x3a11, 0x60 );   // fast zone high         
//	OV5647_reg_write( 0x3a1f, 0x28 );	// fast zone low
	    
	OV5647_reg_write( 0x4004, 0x04 );   /* black line number   */       
     
         
/*	OV5647_reg_write( 0x4050, 0x6e );  */	              
/*	OV5647_reg_write( 0x4051, 0x8f );  */
	OV5647_reg_write( 0x0100 ,0x01 );	// wake up from software standby		
} 

void OV5647_Sensor_Init_1M(void)		
	{

  
	OV5647_reg_write( 0x3821, 0x00 );	// ISP miror off, Sensor mirror off, bin off   		
	OV5647_reg_write( 0x3820, 0x41 );  	// ISP flip off, Sensor flip off, bin on
				
 


	
	/*以下是配置成1296*972格式的配置*/			   
           

	//窗口大小配置	
	OV5647_reg_write( 0x3800, 0x00 ); 	//X Start
	OV5647_reg_write( 0x3801, 0x00 );                
	OV5647_reg_write( 0x3802, 0x00 );   //Y Start             
	OV5647_reg_write( 0x3803, 0x00 );                
	OV5647_reg_write( 0x3804, 0x0a );   //X End 	2623            
	OV5647_reg_write( 0x3805, 0x3f );                
	OV5647_reg_write( 0x3806, 0x07 );   //Y End     1955        
	OV5647_reg_write( 0x3807, 0xa3 ); 
	//偏移量配置
 	OV5647_reg_write( 0x3810, 0x00 );
	OV5647_reg_write( 0x3811, 0x04 );  //X偏移量
	OV5647_reg_write( 0x3812, 0x00 );	
	OV5647_reg_write( 0x3813, 0x02 );  //Y偏移量 	
	//输出图像大小配置
	OV5647_reg_write( 0x3808, 0x05 );   //DVPHO = 1296             
	OV5647_reg_write( 0x3809, 0x10 );   //DVPHO            
	OV5647_reg_write( 0x380a, 0x03 );  	//DVPVO = 972              
	OV5647_reg_write( 0x380b, 0xcc ); 	//DVPHO
	
	OV5647_reg_write( 0x3612, 0x59 );   //analog control             
	OV5647_reg_write( 0x3618, 0x00 );  	//analog control 
	//帧率:10FPS
	OV5647_reg_write( 0x380c, 0x12 );  	//HTS     4800       
	OV5647_reg_write( 0x380d, 0xc0 );                
	OV5647_reg_write( 0x380e, 0x03 );   //VTS     1000       
	OV5647_reg_write( 0x380f, 0xe8 );  
	//子采样配置
	OV5647_reg_write( 0x3814, 0x31 ); 	// timing X inc             
	OV5647_reg_write( 0x3815, 0x31 ); 	// timing Y inc	
	
	OV5647_reg_write( 0x3708, 0x64 );  	// analog control            
	OV5647_reg_write( 0x3709, 0x52 );  	// analog control  	
	


	

	/*banding filters are calculated automatically in camera driver */           
	OV5647_reg_write( 0x3a08, 0x01 );   // B50 step             
	OV5647_reg_write( 0x3a09, 0x27 );   // B50 step             
	OV5647_reg_write( 0x3a0a, 0x00 );   // B60 step            
	OV5647_reg_write( 0x3a0b, 0xf6 );   // B60 step             
	OV5647_reg_write( 0x3a0d, 0x04 );   // 60Hz max band             
	OV5647_reg_write( 0x3a0e, 0x03 );	// 50Hz max band
	
	/* AGC AEC target */              
	OV5647_reg_write( 0x3a0f, 0x58 );	// stable in high            
	OV5647_reg_write( 0x3a10, 0x50 );   // stable in low          
	OV5647_reg_write( 0x3a1b, 0x58 );   // stable out high          
	OV5647_reg_write( 0x3a1e, 0x50 );   // stable out low          
	OV5647_reg_write( 0x3a11, 0x60 );   // fast zone high         
	OV5647_reg_write( 0x3a1f, 0x28 );	// fast zone low
	    
	OV5647_reg_write( 0x4004, 0x02 );   /* black line number   */       
     
	OV5647_reg_write( 0x4837, 0x24 );     /*pclk period */          
	/*	OV5647_reg_write( 0x4050, 0x6e );  */	              
	/*	OV5647_reg_write( 0x4051, 0x8f );  */
	OV5647_reg_write( 0x0100 ,0x01 );	// wake up from software standby	
	

}	
	
	
void OV5647_Sensor_Init_VGA(void)
		
{
 
	OV5647_reg_write( 0x3821, 0x00 );	// ISP miror off, Sensor mirror off, bin off   		
	OV5647_reg_write( 0x3820, 0x41 );  	// ISP flip off, Sensor flip off, bin on
				
	OV5647_reg_write( 0x3827, 0xec );   //analog control       
	OV5647_reg_write( 0x370c, 0x03 );   //analog control	 
	OV5647_reg_write( 0x3612, 0x59 );   //analog control             
	OV5647_reg_write( 0x3618, 0x00 );  	//analog control 

	
	/*以下是配置成vga格式的配置*/			   
	OV5647_reg_write( 0x3708, 0x64 );  	// analog control            
	OV5647_reg_write( 0x3709, 0x52 );  	// analog control             

	//窗口大小配置	
	OV5647_reg_write( 0x3800, 0x00 ); 	//X Start
	OV5647_reg_write( 0x3801, 0x00 );                
	OV5647_reg_write( 0x3802, 0x00 );   //Y Start             
	OV5647_reg_write( 0x3803, 0x00 );                
	OV5647_reg_write( 0x3804, 0x0a );   //X End 	2623            
	OV5647_reg_write( 0x3805, 0x3f );                
	OV5647_reg_write( 0x3806, 0x07 );   //Y End     1955        
	OV5647_reg_write( 0x3807, 0xa3 ); 
	//输出图像大小配置
	OV5647_reg_write( 0x3808, 0x02 );   //DVPHO = 640             
	OV5647_reg_write( 0x3809, 0x80 );   //DVPHO            
	OV5647_reg_write( 0x380a, 0x01 );  	//DVPVO = 480              
	OV5647_reg_write( 0x380b, 0xE0 ); 	//DVPHO
	//帧率
	OV5647_reg_write( 0x380c, 0x07 );  	//HTS     1896        
	OV5647_reg_write( 0x380d, 0x68 );                
	OV5647_reg_write( 0x380e, 0x03 );   //VTS     984       
	OV5647_reg_write( 0x380f, 0xd8 );  
	//偏移量配置
 	OV5647_reg_write( 0x3810, 0x00 );
	OV5647_reg_write( 0x3811, 0x04 );  //X偏移量
	OV5647_reg_write( 0x3812, 0x00 );	
	OV5647_reg_write( 0x3813, 0x02 );  //Y偏移量              
	OV5647_reg_write( 0x3814, 0x71 ); 	// timing X inc             
	OV5647_reg_write( 0x3815, 0x71 ); 	// timing Y inc
	

	/*banding filters are calculated automatically in camera driver */           
	OV5647_reg_write( 0x3a08, 0x01 );   // B50 step             
	OV5647_reg_write( 0x3a09, 0x27 );   // B50 step             
	OV5647_reg_write( 0x3a0a, 0x00 );   // B60 step            
	OV5647_reg_write( 0x3a0b, 0xf6 );   // B60 step             
	OV5647_reg_write( 0x3a0d, 0x04 );   // 60Hz max band             
	OV5647_reg_write( 0x3a0e, 0x03 );	// 50Hz max band
	
	/* AGC AEC target */              
	OV5647_reg_write( 0x3a0f, 0x58 );	// stable in high            
	OV5647_reg_write( 0x3a10, 0x50 );   // stable in low          
	OV5647_reg_write( 0x3a1b, 0x58 );   // stable out high          
	OV5647_reg_write( 0x3a1e, 0x50 );   // stable out low          
	OV5647_reg_write( 0x3a11, 0x60 );   // fast zone high         
	OV5647_reg_write( 0x3a1f, 0x28 );	// fast zone low
	//black level calibration			
 	             
	OV5647_reg_write( 0x4004, 0x02 ); 
	
	OV5647_reg_write( 0x4837, 0x24 );     /*pclk period */ 	
	/*	OV5647_reg_write( 0x4050, 0x6e );                
	OV5647_reg_write( 0x4051, 0x8f ); 	*/	
	OV5647_reg_write( 0x0100 ,0x01 );	// wake up from software standby	

	#if 0
	OV5647_reg_write( 0x5000, 0x86 );   /* enable LENC registers */
	OV5647_reg_write( 0x5800, 0x00 );
	OV5647_reg_write( 0x5801, 0x00 );
	OV5647_reg_write( 0x5802, 0x00 );
	OV5647_reg_write( 0x5803, 0x00 );
	OV5647_reg_write( 0x5804, 0x00 );
	OV5647_reg_write( 0x5805, 0x00 );
	OV5647_reg_write( 0x5806, 0x00 );
	OV5647_reg_write( 0x5807, 0x00 );
	OV5647_reg_write( 0x5808, 0x00 );
	OV5647_reg_write( 0x5809, 0x00 );
	OV5647_reg_write( 0x580a, 0x00 );
	OV5647_reg_write( 0x580b, 0x00 );
	OV5647_reg_write( 0x580c, 0x00 );
	OV5647_reg_write( 0x580d, 0x00 );
	OV5647_reg_write( 0x580e, 0x00 );
	OV5647_reg_write( 0x580f, 0x00 );
	OV5647_reg_write( 0x5810, 0x00 );
	OV5647_reg_write( 0x5811, 0x00 );
	OV5647_reg_write( 0x5812, 0x00 );
	OV5647_reg_write( 0x5813, 0x00 );
	OV5647_reg_write( 0x5814, 0x00 );
	OV5647_reg_write( 0x5815, 0x00 );
	OV5647_reg_write( 0x5816, 0x00 );
	OV5647_reg_write( 0x5817, 0x00 );
	OV5647_reg_write( 0x5818, 0x00 );
	OV5647_reg_write( 0x5819, 0x00 );
	OV5647_reg_write( 0x581a, 0x00 );
	OV5647_reg_write( 0x581b, 0x00 );
	OV5647_reg_write( 0x581c, 0x00 );
	OV5647_reg_write( 0x581d, 0x00 );
	OV5647_reg_write( 0x581e, 0x00 );
	OV5647_reg_write( 0x581f, 0x00 );
	OV5647_reg_write( 0x5820, 0x00 );
	OV5647_reg_write( 0x5821, 0x00 );
	OV5647_reg_write( 0x5822, 0x00 );
	OV5647_reg_write( 0x5823, 0x00 );
	OV5647_reg_write( 0x5824, 0xfe );
	OV5647_reg_write( 0x5825, 0xfe );
	OV5647_reg_write( 0x5826, 0xfe );
	OV5647_reg_write( 0x5827, 0xfe );
	OV5647_reg_write( 0x5828, 0xfe );
	OV5647_reg_write( 0x5829, 0xfe );
	OV5647_reg_write( 0x582a, 0xed );
	OV5647_reg_write( 0x582b, 0xed );
	OV5647_reg_write( 0x582c, 0xed );
	OV5647_reg_write( 0x582d, 0xfe );
	OV5647_reg_write( 0x582e, 0xfe );
	OV5647_reg_write( 0x582f, 0xed );
	OV5647_reg_write( 0x5830, 0xdc );
	OV5647_reg_write( 0x5831, 0xed );
	OV5647_reg_write( 0x5832, 0xfe );
	OV5647_reg_write( 0x5833, 0xfe );
	OV5647_reg_write( 0x5834, 0xed );
	OV5647_reg_write( 0x5835, 0xed );
	OV5647_reg_write( 0x5836, 0xed );
	OV5647_reg_write( 0x5837, 0xfe );
	OV5647_reg_write( 0x5838, 0xfe );
	OV5647_reg_write( 0x5839, 0xfe );
	OV5647_reg_write( 0x583a, 0xfe );
	OV5647_reg_write( 0x583b, 0xfe );
	OV5647_reg_write( 0x583c, 0xfe );
	OV5647_reg_write( 0x583d, 0xce );
	#endif
}	
void OV5647_Sensor_Init_03M(void)		
{

	/*3036的值除60为倍频数*/
	OV5647_reg_write(0x3035 , 0x21 );	// PLL
	OV5647_reg_write(0x3036 , 0x3c );	// PLL
	OV5647_reg_write(0x303c , 0x12 );	// PLL
  
	OV5647_reg_write( 0x3821, 0x00 );	// ISP miror off, Sensor mirror off, bin off   		
	OV5647_reg_write( 0x3820, 0x41 );  	// ISP flip off, Sensor flip off, bin on
				
	OV5647_reg_write( 0x3827, 0xec );   //analog control       
	OV5647_reg_write( 0x370c, 0x03 );   //analog control	 
	OV5647_reg_write( 0x3612, 0x59 );   //analog control             
	OV5647_reg_write( 0x3618, 0x00 );  	//analog control 
	//awb
	OV5647_reg_write( 0x5000, 0x06 );   /*lenc off, bpc on, wpc on */          
	OV5647_reg_write( 0x5001, 0x01 );   /*AWB Enable */            
	OV5647_reg_write( 0x5002, 0x41 );   /*AWB Gain Enable */             
	OV5647_reg_write( 0x5003, 0x08 );   /*buf_en */             
	OV5647_reg_write( 0x5a00, 0x08 );   /*Debug mode ensable */

	//增益             
	OV5647_reg_write( 0x301d, 0xf0 );                
	OV5647_reg_write( 0x3a18, 0x00 );   /*Bit[1:0]: gain_ceiling[9:8]*/              
	OV5647_reg_write( 0x3a19, 0xf8 );   /* Bit[1:0]: gain_ceiling[7:0]  */             
	OV5647_reg_write( 0x3c01, 0x80 );                
	OV5647_reg_write( 0x3b07, 0x0c ); 
	
	/*以下是配置成vga格式的配置*/			   
	OV5647_reg_write( 0x3708, 0x64 );  	// analog control            
	OV5647_reg_write( 0x3709, 0x52 );  	// analog control             

	//窗口大小配置	
	OV5647_reg_write( 0x3800, 0x00 ); 	//X Start
	OV5647_reg_write( 0x3801, 0x00 );                
	OV5647_reg_write( 0x3802, 0x00 );   //Y Start             
	OV5647_reg_write( 0x3803, 0x00 );                
	OV5647_reg_write( 0x3804, 0x0a );   //X End 	2623            
	OV5647_reg_write( 0x3805, 0x3f );                
	OV5647_reg_write( 0x3806, 0x07 );   //Y End     1955        
	OV5647_reg_write( 0x3807, 0xa3 ); 
	//输出图像大小配置
	OV5647_reg_write( 0x3808, 0x02 );   //DVPHO = 648             
	OV5647_reg_write( 0x3809, 0x88 );   //DVPHO            
	OV5647_reg_write( 0x380a, 0x01 );  	//DVPVO = 486              
	OV5647_reg_write( 0x380b, 0xE6 ); 	//DVPHO
	//帧率
	OV5647_reg_write( 0x380c, 0x07 );  	//HTS     1896        
	OV5647_reg_write( 0x380d, 0x68 );                
	OV5647_reg_write( 0x380e, 0x03 );   //VTS     984       
	OV5647_reg_write( 0x380f, 0xd8 );  
	//偏移量配置
	// 	OV5647_reg_write( 0x3810, 0x00 );
	OV5647_reg_write( 0x3811, 0x04 );  //X偏移量
	//	OV5647_reg_write( 0x3812, 0x00 );	
	OV5647_reg_write( 0x3813, 0x02 );  //Y偏移量              
	OV5647_reg_write( 0x3814, 0x71 ); 	// timing X inc             
	OV5647_reg_write( 0x3815, 0x71 ); 	// timing Y inc
	
	/*analog control*/
	OV5647_reg_write( 0x3630, 0x2e );                
	OV5647_reg_write( 0x3632, 0xe2 );                
	OV5647_reg_write( 0x3633, 0x23 );                
	OV5647_reg_write( 0x3634, 0x44 );                
	OV5647_reg_write( 0x3636, 0x06 );                
	OV5647_reg_write( 0x3620, 0x64 );                
	OV5647_reg_write( 0x3621, 0xe0 );                
	OV5647_reg_write( 0x3600, 0x37 );                
	OV5647_reg_write( 0x3704, 0xa0 );                
	OV5647_reg_write( 0x3703, 0x5a );               
	OV5647_reg_write( 0x3715, 0x78 );                
	OV5647_reg_write( 0x3717, 0x01 );                
	OV5647_reg_write( 0x3731, 0x02 );                
	OV5647_reg_write( 0x370b, 0x60 );                
	OV5647_reg_write( 0x3705, 0x1a );                
	
	
	OV5647_reg_write( 0x3f05, 0x02 );                
	OV5647_reg_write( 0x3f06, 0x10 );                
	OV5647_reg_write( 0x3f01, 0x0a );
	/*banding filters are calculated automatically in camera driver */           
	OV5647_reg_write( 0x3a08, 0x01 );   // B50 step             
	OV5647_reg_write( 0x3a09, 0x27 );   // B50 step             
	OV5647_reg_write( 0x3a0a, 0x00 );   // B60 step            
	OV5647_reg_write( 0x3a0b, 0xf6 );   // B60 step             
	OV5647_reg_write( 0x3a0d, 0x04 );   // 60Hz max band             
	OV5647_reg_write( 0x3a0e, 0x03 );	// 50Hz max band
	
	/* AGC AEC target */              
	OV5647_reg_write( 0x3a0f, 0x58 );	// stable in high            
	OV5647_reg_write( 0x3a10, 0x50 );   // stable in low          
	OV5647_reg_write( 0x3a1b, 0x58 );   // stable out high          
	OV5647_reg_write( 0x3a1e, 0x50 );   // stable out low          
	OV5647_reg_write( 0x3a11, 0x60 );   // fast zone high         
	OV5647_reg_write( 0x3a1f, 0x28 );	// fast zone low
	//black level calibration			
	OV5647_reg_write( 0x4001, 0x02 );   // BLC start line
	OV5647_reg_write( 0x4000, 0x09 );   // BLC enable  	             
	OV5647_reg_write( 0x4004, 0x02 );                
          
	OV5647_reg_write( 0x4837, 0x24 );     /*pclk period */          
	/*	OV5647_reg_write( 0x4050, 0x6e );                
	OV5647_reg_write( 0x4051, 0x8f ); 	*/	
	OV5647_reg_write( 0x0100 ,0x01 );	// wake up from software standby	
	/*-----------------------------------------------------------------------*/
	/*-----------------------------------------------------------------------*/
	#if 0
	OV5647_reg_write( 0x5000, 0x86 );   /* enable LENC registers */
	OV5647_reg_write( 0x5800, 0x00 );
	OV5647_reg_write( 0x5801, 0x00 );
	OV5647_reg_write( 0x5802, 0x00 );
	OV5647_reg_write( 0x5803, 0x00 );
	OV5647_reg_write( 0x5804, 0x00 );
	OV5647_reg_write( 0x5805, 0x00 );
	OV5647_reg_write( 0x5806, 0x00 );
	OV5647_reg_write( 0x5807, 0x00 );
	OV5647_reg_write( 0x5808, 0x00 );
	OV5647_reg_write( 0x5809, 0x00 );
	OV5647_reg_write( 0x580a, 0x00 );
	OV5647_reg_write( 0x580b, 0x00 );
	OV5647_reg_write( 0x580c, 0x00 );
	OV5647_reg_write( 0x580d, 0x00 );
	OV5647_reg_write( 0x580e, 0x00 );
	OV5647_reg_write( 0x580f, 0x00 );
	OV5647_reg_write( 0x5810, 0x00 );
	OV5647_reg_write( 0x5811, 0x00 );
	OV5647_reg_write( 0x5812, 0x00 );
	OV5647_reg_write( 0x5813, 0x00 );
	OV5647_reg_write( 0x5814, 0x00 );
	OV5647_reg_write( 0x5815, 0x00 );
	OV5647_reg_write( 0x5816, 0x00 );
	OV5647_reg_write( 0x5817, 0x00 );
	OV5647_reg_write( 0x5818, 0x00 );
	OV5647_reg_write( 0x5819, 0x00 );
	OV5647_reg_write( 0x581a, 0x00 );
	OV5647_reg_write( 0x581b, 0x00 );
	OV5647_reg_write( 0x581c, 0x00 );
	OV5647_reg_write( 0x581d, 0x00 );
	OV5647_reg_write( 0x581e, 0x00 );
	OV5647_reg_write( 0x581f, 0x00 );
	OV5647_reg_write( 0x5820, 0x00 );
	OV5647_reg_write( 0x5821, 0x00 );
	OV5647_reg_write( 0x5822, 0x00 );
	OV5647_reg_write( 0x5823, 0x00 );
	OV5647_reg_write( 0x5824, 0xfe );
	OV5647_reg_write( 0x5825, 0xfe );
	OV5647_reg_write( 0x5826, 0xfe );
	OV5647_reg_write( 0x5827, 0xfe );
	OV5647_reg_write( 0x5828, 0xfe );
	OV5647_reg_write( 0x5829, 0xfe );
	OV5647_reg_write( 0x582a, 0xed );
	OV5647_reg_write( 0x582b, 0xed );
	OV5647_reg_write( 0x582c, 0xed );
	OV5647_reg_write( 0x582d, 0xfe );
	OV5647_reg_write( 0x582e, 0xfe );
	OV5647_reg_write( 0x582f, 0xed );
	OV5647_reg_write( 0x5830, 0xdc );
	OV5647_reg_write( 0x5831, 0xed );
	OV5647_reg_write( 0x5832, 0xfe );
	OV5647_reg_write( 0x5833, 0xfe );
	OV5647_reg_write( 0x5834, 0xed );
	OV5647_reg_write( 0x5835, 0xed );
	OV5647_reg_write( 0x5836, 0xed );
	OV5647_reg_write( 0x5837, 0xfe );
	OV5647_reg_write( 0x5838, 0xfe );
	OV5647_reg_write( 0x5839, 0xfe );
	OV5647_reg_write( 0x583a, 0xfe );
	OV5647_reg_write( 0x583b, 0xfe );
	OV5647_reg_write( 0x583c, 0xfe );
	OV5647_reg_write( 0x583d, 0xce );
	#endif
}

/*----------------------------------------------------------- 
** Exposure=0x3500 [3:0]×4096+0x3501[7:0]×256+0x3502[7: 4] 
** The maximum exposure value is limited by VTS defined by register 0x380e and 0x380f.
** Maximum Exposure <= VTS -4
------------------------------------------------------------*/
int OV5647_get_shutter()
{
	// read shutter, in number of line period
	int shutter;
	shutter = (OV5647_reg_read(0x03500) & 0x0f);
	shutter = (shutter<<8) + OV5647_reg_read(0x3501);
	shutter = (shutter<<4) + (OV5647_reg_read(0x3502)>>4);
	return shutter;
}

int OV5647_set_shutter(int shutter)
{
	
	int temp;
/* 	if (capture_shutter > (capture_VTS - 4)) 
	{
	capture_VTS = capture_shutter + 4;
	} */

	// write shutter, in number of line period
	shutter = shutter & 0xffff;
	temp = shutter & 0x0f;
	temp = temp<<4;
	OV5647_reg_write(0x3502, temp);
	temp = shutter & 0xfff;
	temp = temp>>4;
	OV5647_reg_write(0x3501, temp);
	temp = shutter>>12;
	OV5647_reg_write(0x3500, temp);
	return 0;
}

int OV5647_set_VTS(int VTS)
{
	// write VTS to registers
	int temp;
	temp = VTS & 0xff;
	OV5647_reg_write(0x380f, temp);
	temp = VTS>>8;
	OV5647_reg_write(0x380e, temp);
	return 0;
}

/*-------------------real gain---------------------------------- 
** Gain=R0x350a[1:0]×16 + R0x350b [7 :0 ]/16
--------------------------------------------------------------*/		
int OV5647_get_gain16()
{
	// read real gain, 16 = 1x
	int gain16;
	gain16 = OV5647_reg_read(0x350a) & 0x03;
	gain16 = (gain16<<8) + OV5647_reg_read(0x350b);
	return gain16;
}

int OV5647_set_gain16(int gain16)
{
	// write real gain, 16 = 1x
	int temp;
	if(gain16 > 0x3d0)
	gain16 = 0x3d0;
	temp = gain16 & 0xff;
	OV5647_reg_write(0x350b, temp);
	temp = gain16>>8;
	OV5647_reg_write(0x350a, temp);
	return 0;
}
	/*系统时钟计算*/
int OV5647_get_SCLK(int XVCLK)
{
	// calculate PCLK
	int SCLK, temp1, temp2, VCO;
	int pre_div02x, div_cnt7b, sdiv0, pll_rdiv, bit_div2x, sclk_div;
	int code pre_div02x_map[] = {
	2, 2, 4, 6, 8, 3, 12, 5, 16, 2, 2, 2, 2, 2, 2, 2};

	int code sdiv0_map[] = {
	16, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15};

	int code pll_rdiv_map[] = {
	1, 2};

	int code bit_div2x_map[] = {
	1, 1, 1, 1, 1, 1, 1, 1, 4, 1, 5, 1, 1, 1, 1, 1};

	int code sclk_div_map[] = {
	1, 2, 4, 1};
	temp1 = OV5647_reg_read(0x3037);
	temp2 = temp1 & 0x0f;
	pre_div02x = pre_div02x_map[temp2];
	temp2 = (temp1>>4) & 0x01;
	pll_rdiv = pll_rdiv_map[temp2];
	temp1 = OV5647_reg_read(0x3036);
	if(temp1 & 0x80)
	{	
	div_cnt7b = (int)(temp1/2)*2;
	}

	else
	div_cnt7b = temp1;
	
	VCO = XVCLK * 2 / pre_div02x * div_cnt7b;
	
	temp1 = OV5647_reg_read(0x3035);
	temp2 = temp1>>4;
	sdiv0 = sdiv0_map[temp2];
	
	temp1 = OV5647_reg_read(0x3034);
	temp2 = temp1 & 0x0f;
	bit_div2x = bit_div2x_map[temp2];
	
	temp1 = OV5647_reg_read(0x3106);
	temp2 = (temp1>>2) & 0x03;
	sclk_div = sclk_div_map[temp2];
	
	SCLK = VCO * 2/ sdiv0 / pll_rdiv / bit_div2x / sclk_div;
	return SCLK;
}
	

